/* -*- c++ -*- */
/*
 * This file is part of the SD Project.
 *
 * Authors: Cleiber Marques da Silva <cleiber@lisha.ufsc.br>
 * 			Odilson Tadeu Valle <odilson@sj.ifsc.edu.br>
 * 
 * SD Project is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the License.
 */

#include <iostream>

#include "window.h"

namespace sd {
	window::window() : omni_thread()
	{
		text_buffer = Gtk::TextBuffer::create();
		start();
	}

	void window::run(void *arg)
	{
		std::cout << "Thread run" << std::endl;

		// Load interface from glade file
		Glib::RefPtr<Gnome::Glade::Xml> widgets = Gnome::Glade::Xml::create("sd.glade");

		widgets->get_widget("window1", main);
		widgets->get_widget("quit", quit);
		widgets->get_widget("about", about);
		widgets->get_widget("textview1", text);
		widgets->get_widget("entry1", entry);
		widgets->get_widget("send", button);
		widgets->get_widget("statusbar1", status);


		// Connect handlers
		quit->signal_activate().connect(sigc::mem_fun(*this, &sd::window::on_quit_activate));
		about->signal_activate().connect(sigc::mem_fun(*this, &sd::window::on_about_activate));
		button->signal_clicked().connect(sigc::mem_fun(*this, &sd::window::on_button_clicked));

		// Show interface
		main->show_all();

		// Gtk main loop
		Gtk::Main::run(*main);
	}

	void window::fill_buffer(std::string buffer)
	{
  		//text_buffer->insert(text_buffer->end(), buffer);
		text_buffer->insert_at_cursor(buffer);
		text->set_buffer(text_buffer);
	}

	void window::on_quit_activate()
	{
		std::cout << "Quit press" << std::endl;
	}

	void window::on_about_activate()
	{
		Gtk::AboutDialog dialog;
		//dialog.set_transient_for(*this);

		dialog.set_name("SD Project");
		dialog.set_version("0.1");
		dialog.set_copyright("Cleiber Marques da Silva");
		dialog.set_comments("This is just an example application.");
		dialog.set_license("GPL");

		std::list<Glib::ustring> list_authors;
		list_authors.push_back("Cleiber Marques da Silva");
		list_authors.push_back("Odilson Tadeu Valle");
		dialog.set_authors(list_authors);

		//m_Dialog.signal_response().connect( sigc::mem_fun(*this, &ExampleWindow::on_about_dialog_response) );

		dialog.show();
		std::cout << "About press" << std::endl;
	}

	void window::on_button_clicked()
	{
		std::cout << "Button clicked" << std::endl;
		std::string mesg = entry->get_text();
		entry->set_text("");
		fill_buffer(mesg);
	}
}
